package com.top.ruizhong.main.cate

import android.graphics.Typeface
import android.widget.TextView
import androidx.fragment.app.Fragment
import androidx.viewpager2.adapter.FragmentStateAdapter
import androidx.viewpager2.widget.ViewPager2
import com.blankj.utilcode.util.BarUtils
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.module.lemlin.base.BaseAbstractFragment
import com.top.ruizhong.R
import kotlinx.android.synthetic.main.fragment_main_cate.*

class MainCateFragment : BaseAbstractFragment() {


    companion object {
        fun newInstance(): MainCateFragment {
            return MainCateFragment()
        }

        private val titles: MutableList<String> = mutableListOf(
            "title1", "title1", "title1", "title1", "title1", "title1", "title1"
        )
    }

    private val cateTitleAdapter =
        object : BaseQuickAdapter<String, BaseViewHolder>(R.layout.item_category_title) {

            var selectedPosition = 0
                set(value) {
                    if (field != value) {
                        field = value
                        notifyDataSetChanged()
                    }
                }

            init {
                setOnItemClickListener { adapter, view, position ->
                    selectedPosition = position
                }
            }

            override fun convert(holder: BaseViewHolder, item: String) {
                holder.getView<TextView>(R.id.rvCategoryTitle).apply {
                    typeface = if (holder.layoutPosition == selectedPosition) {
                        Typeface.defaultFromStyle(Typeface.BOLD)
                    } else {
                        Typeface.defaultFromStyle(Typeface.NORMAL)
                    }
                    textSize = if (holder.layoutPosition == selectedPosition)
                        16f else 14f
                }
                holder.itemView.isSelected = holder.layoutPosition == selectedPosition
            }
        }

    override val layoutResId: Int
        get() = R.layout.fragment_main_cate

    override fun initView() {

        BarUtils.addMarginTopEqualStatusBarHeight(frameCategoryBar)

        rvCategoryTitle.adapter = cateTitleAdapter.apply {
            setNewInstance(titles)
        }

        vp2CategoryBody.adapter = object : FragmentStateAdapter(requireActivity()) {

            override fun getItemCount(): Int = titles.size

            override fun createFragment(position: Int): Fragment {
                return CateVideoFragment.newInstance()
            }
        }

        vp2CategoryBody.registerOnPageChangeCallback(object : ViewPager2.OnPageChangeCallback() {
            override fun onPageSelected(position: Int) {
                cateTitleAdapter.selectedPosition = position
            }
        })

        cateTitleAdapter.setOnItemClickListener { _, _, position ->
            cateTitleAdapter.selectedPosition = position
            vp2CategoryBody.currentItem = position
        }
    }

    override fun initData() {

    }

}